package com.cb.web.wasf.sample.web;

import com.cb.web.wasf.UnExpectedEventException;
import com.cb.web.wasf.Request;
import com.cb.web.wasf.State;

/**
 * @author CBO
 */
public abstract class AbstractMenuSupportState extends State {

    public AbstractMenuSupportState() {
        super();
    }

    public State menuClick(Request request) {
        return processMenuClickEventSource(request, request.getEventSource());
    }

    public State processMenuClickEventSource(Request request, String eventSource) {
        if (MenuEventHandler.USER_ADMINISTRATION.equals(eventSource)
                && request.getSession().getPrincipal().hasRole(ManagerLocator.getRoleManager().getUserAdministrationRole().getName())) {
            return new UserListState();
        } else if (MenuEventHandler.CARD.equals(request.getEventSource())) {
            return new CartState();
        } else if (MenuEventHandler.LOGOUT.equals(request.getEventSource())) {
            request.getSession().requestDestruction();
            return new LoginState();
        } else {
            throw new UnExpectedEventException(request.getEvent(), request.getEventSource());
        }
    }
}
